
#!/usr/bin/python
# -*- coding: iso-8859-1 -*-

import Tkinter
import os

class serv_console_tk(Tkinter.Tk):
	def __init__(self,parent):
		Tkinter.Tk.__init__(self,parent)
		self.parent=parent
		self.initialize()

	def initialize(self):
		self.grid()
		

		#Label del Dominio		
		self.labelVariable = Tkinter.StringVar()		
		self.labelVariable.set("Dominio")		
		dlabel=Tkinter.Label(self,textvariable=self.labelVariable,anchor="w",fg="white",bg="blue")
        	dlabel.grid(column=0,row=0,columnspan=2,sticky='EW') 		
		
		#Label del incio del pool de clientes		
		self.labelVariable = Tkinter.StringVar()		
		self.labelVariable.set("Inicio")		
		ilabel=Tkinter.Label(self,textvariable=self.labelVariable,anchor="w",fg="white",bg="blue")
        	ilabel.grid(column=0,row=1,columnspan=2,sticky='EW') 

		#Label del final del pool de clientes		
		self.labelVariable = Tkinter.StringVar()		
		self.labelVariable.set("Final")		
		flabel=Tkinter.Label(self,textvariable=self.labelVariable,anchor="w",fg="white",bg="blue")
        	flabel.grid(column=0,row=2,columnspan=2,sticky='EW') 
        
		#Textfield del parametro Dominio		
		self.domValue = Tkinter.StringVar()		
		self.entry = Tkinter.Entry(self,textvariable=self.domValue)
        	self.entry.grid(column=1,row=0, sticky='EW')
		self.domValue.set(u"1")
		
		#Textfield del inicio del pool de clientes
		self.initValue=Tkinter.StringVar()		
		self.entry = Tkinter.Entry(self,textvariable=self.initValue)
        	self.entry.grid(column=1,row=1, sticky='EW')
		self.initValue.set(u"0")		

		#Textfield del final del pool de clientes
		self.endValue=Tkinter.StringVar()		
		self.entry = Tkinter.Entry(self,textvariable=self.endValue)
        	self.entry.grid(column=1,row=2, sticky='EW')
		self.endValue.set(u"1024")
	
		#Boton ejecutar
		run=Tkinter.Button(self,text=u"Ejecutar",command=self.Start_ButtonClick)
	        run.grid(column=0,row=3, sticky="W")
		
		#Boton detener
		stop=Tkinter.Button(self,text=u"Detener",command=self.Stop_ButtonClick)
	        stop.grid(column=1,row=3, sticky="E")
		
	def Start_ButtonClick(self):
			
		if(os.system('g++ Server_packer.cpp -o sp')):
			pass
		elif(os.system('g++ Server_unpacker.cpp -o su')):
			pass
		elif(os.system('g++ Quimera_protocol_serv.cpp  -o qps')):
 			pass
                elif(os.system('./qps 3 '+self.domValue.get()+' '+self.initValue.get()+' '+self.endValue.get()+' > /dev/tty1 &')):
                        pass	       
                elif(os.system('./sp > /dev/tty2 &')):
			pass
		elif(os.system('./su > /dev/tty3 &')):
			pass				
		

	def Stop_ButtonClick(self):
		self.L=[]		
		self.L.append(os.popen('pidof sp').readline())
		self.L.append(os.popen('pidof su').readline())
		self.L.append(os.popen('pidof qps').readline())	
		for item in self.L:
			os.system('kill '+item)
			

	

if __name__=="__main__":
	app=serv_console_tk(None)
	app.title('Consola del servidor Quimera')
	app.mainloop()




